﻿using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shop.Home.Repositories
{
    /// <summary>
    /// 生成订单时对商品的操作数据访问层
    /// </summary>
    public class WaresInventoryAccess
    {
        private Database DB = new Database("ConnString");


        /// <summary>
        /// 根据商品ID获取商品库存
        /// </summary>
        /// <param name="WID"></param>
        /// <returns></returns>
        public Shop.Wares.Models.Wares GetWaresStock(int WID)
        {
            Sql sql = Sql.Builder.Append("select * from Shop_Wares where WID = @0", WID);
            return DB.FirstOrDefault<Shop.Wares.Models.Wares>(sql);
        }
        /// <summary>
        /// 减少商品库存
        /// </summary>
        /// <param name="WID">商品ID</param>
        /// <param name="BuyNum">减少的数量</param>
        /// <returns></returns>
        public int ReduceWares(int WID,int BuyNum)
        {
            Sql sql = Sql.Builder.Append("update Shop_Wares set Stock = (Stock - @0) where WID = @1", BuyNum, WID);
            return DB.Execute(sql);
        }
        /// <summary>
        /// 增加商品销量
        /// </summary>
        /// <param name="WID"></param>
        /// <param name="BuyNum"></param>
        /// <returns></returns>
        public int AddSaleNum(int WID,int BuyNum)
        {
            Sql sql = Sql.Builder.Append("update Shop_Wares set SaleNum = (SaleNum + @0) where WID = @1", BuyNum, WID);
            return DB.Execute(sql);
        }

    }
}
